package com.gxaedu.rbac.service.impl;

import com.gxaedu.rbac.dto.ResponseData;
import com.gxaedu.rbac.dto.ResultData;
import com.gxaedu.rbac.mapper.UserMapper;
import com.gxaedu.rbac.pojo.User;
import com.gxaedu.rbac.service.UserService;
import com.gxaedu.rbac.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:Teacher黄
 * @date:Created at 2020/04/20
 */
@Service
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;


    @Override
    public ResultData login(User user, HttpServletRequest request) {
        // 写登陆的逻辑
        // 从数据库获取用户
        User dbUser = userMapper.findByUserName(user.getUserName());
        // 判断用户是否存在
        if(dbUser == null){
            return ResponseData.error(3001,"对不起没有该用户!");
        }
        // 判断密码
        if(!dbUser.getUserPassword().equals(MD5Util.MD55(user.getUserPassword()))){
            return ResponseData.error(3002,"密码错误!");
        }
        // 登陆成功将数据保存在session中
        request.getSession().setAttribute("user",dbUser);
        request.getSession().setAttribute("userName",dbUser.getUserName());

        return ResponseData.success("登陆成功!",null);
    }
}
